package com.shuqi.model.parse.parser;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import com.shuqi.common.utils.Log4an;
import com.shuqi.model.bean.FeedBackInfo;
import com.shuqi.model.bean.FeedBacksInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * 反馈bean
 * 
 * @author zhuchen
 * 
 */
public class FeedBackParser extends HandlerBase {

    private List<FeedBackInfo> list;
    private FeedBackInfo feedBackInfo;
    private FeedBacksInfo feedBacksInfo;

    @Override
    public Object getParsedData() {
        // TODO Auto-generated method stub
        return feedBacksInfo;
    }

    @Override
    public void setDocumentLocator(Locator locator) {
        // TODO Auto-generated method stub
        list = new ArrayList<FeedBackInfo>();
        super.setDocumentLocator(locator);
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(uri, localName, qName, attributes);
        if ("FeedBacks".equals(localName)) {
            feedBacksInfo = new FeedBacksInfo();
            feedBacksInfo.setCode(isNull(attributes, "code"));
            feedBacksInfo.setMessage(isNull(attributes, "message"));
            feedBacksInfo.setTotalCount(isNull(attributes, "totalCount"));
        } else if ("FeedBack".equals(localName)) {
            feedBackInfo = new FeedBackInfo();
            feedBackInfo.setId(isNull(attributes, "id"));
            try {
                feedBackInfo.setContent(URLDecoder.decode(isNull(attributes, "content"), "utf-8"));
                feedBackInfo.setReply(URLDecoder.decode(isNull(attributes, "reply"), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            feedBackInfo.setTime(isNull(attributes, "time") + "000");
            list.add(feedBackInfo);
            feedBacksInfo.setList(list);
        }

    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
    }
}
